<template>
  <div class='container'>
 <el-form ref="form"  label-width="80px"  :model="form" :rules="rules">
    <el-form-item label="所属学科"  v-if="$route.path===$route.fullPath" >
    <el-select style="width:100%"  placeholder="请选择"  v-model="form.region" >
      <el-option  v-for="(item,i) in table" :key="i" :label='item.subjectName' :value="item.subjectID"></el-option>
    </el-select>
  </el-form-item>
    <el-form-item label="目录名称" prop="name" v-if="$route.query.id!==''">
      <el-input v-model="form.name"  placeholder="请输入目录名称" />
    </el-form-item>
      <el-form-item class="aaaa">
    <el-button  @click="cancel">取消</el-button>

    <el-button type="primary" @click="onSubmit($route.query.id)">确认</el-button>

  </el-form-item>
 </el-form>
  </div>
</template>
<script>
import { list as getlist, add as addlist, update } from '../../api/hmmm/tags.js'
export default {
  name: 'uuuu',
  created () {
    this.getlist()
  },
  props: {
    tableData: {
      type: Array,
      default: () => []
    },
    isEind: {
      type: Boolean
    },
    sj: {
      type: Object
    }

  },
  data () {
    return {

      form: {
        region: '',
        name: ''

      },
      table: [],
      pp: {
        page: 1,
        pagesize: 100
      },
      rules: {
        name: [{ required: true, message: '目录名称不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    // 清空form
    clear () {
      this.form.region = ''
      this.form.name = ''
      this.$refs.form.resetFields()
    },
    cancel () {
      this.$emit('off')
    },
    // 从学科页面跳转过来点击确定
    async onSubmit () {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          if (this.$route.path !== this.$route.fullPath) {
            if (this.isEind) {
              try {
                const res = await addlist({ id: null, subjectID: Number(this.$route.query.id), tagName: this.form.name })
                this.$emit('Hupdate')
                this.$message.success('恭喜您添加成功')
                // eslint-disable-next-line eqeqeq
                if (res.status == 200) {
                  this.$emit('off')
                }
              } catch (err) {
                console.log(err)
              }
            } else {
              this.Hupdate()
              this.$emit('shut')
              this.$message.success('恭喜您修改成功')
            }
          } else {
            if (this.isEind) {
              this.$emit('off')
              this.add()
              this.$emit('Hupdate')
            } else {
              this.Zupdate()
              this.$emit('new')
              this.$emit('connection')
              this.$message.success('恭喜您，修改成功')
            }
          }
        }
      })
    },
    // 从左侧菜单过来修改
    async Zupdate () {
      try {
        const res = await update({ id: this.sj.id, subjectID: this.form.region, tagName: this.form.name })
        console.log(res)
      } catch (err) {
        console.log(err)
      }
    },
    async Hupdate () {
      try {
        const res = await update({ ...this.sj, tagName: this.form.name })
        console.log(res)
      } catch (err) {
        console.log(err)
      }
    },
    async add () {
      try {
        const res = await addlist({ subjectID: this.form.region, tagName: this.form.name })
        console.log(res)
        this.$message.success('恭喜你添加成功')
        this.$emit('nova')
      } catch (err) {
        console.log(err)
      }
    },
    async getlist () {
      try {
        const res = await getlist(this.pp)
        console.log(res)
        const aaa = res.data.items
        this.table = aaa
      } catch (err) {
        console.log(err)
      }
    }

  }
}
</script>
<style lang="scss" scoped>
.aaaa {
  text-align: right;
}
</style>
